﻿//弹层选择控件
(function ($, ibpm) {
    "use strict";

    // 弹层列表选择
    $.BPM_GirdSelect = {
        init: function ($self) {
            var dfop = $self[0]._MGirdSelect.dfop;
            $self.addClass('M-formselect');
            $self.attr('type', 'GirdSelect');
            var $input = $('<span>' + dfop.placeholder + '</span><i class="fa ' + dfop.icon + '"></i><div class="clear-btn">清空</div>');
            $self.on('click', $.BPM_GirdSelect.click);
            $self.html($input);

            if (dfop.Rule) {
                dfop.PageUrl = top.$.rootUrl + '/Home/GirdSelectPage?dfopid=' + dfop.id + '&param=' + dfop.param['itemCode'];
                dfop._loaded = true;
            }
            else {
                dfop.PageUrl = top.$.rootUrl + '/WorkDone/GirdSelectIndex?dfopid=' + dfop.id;

                // 异步加载数据
                ibpm.httpAsync('GET', dfop.url, dfop.param, function (data) {
                    dfop._loaded = true;
                    dfop._data = data || [];
                });
            }

            top.BPMGirdSelect = top.BPMGirdSelect || {};
            top.BPMGirdSelect[dfop.id] = dfop;
        },
        click: function (e) {
            var $self = $(this);
            var dfop = $self[0]._MGirdSelect.dfop;
            var et = e.target || e.srcElement;
            var $et = $(et);
            if ($et.hasClass('clear-btn')) {
                dfop._itemValue = { value: "", text: dfop.placeholder };
                $self.removeClass('selected');
                $self.find('span').text(dfop._itemValue.text);
                if (!!dfop.select) {
                    dfop.select(dfop._itemValue);
                }
            }
            else {
                var value = dfop._itemValue ? dfop._itemValue.value : "";
                //这里是弹出iframe层，不能与现有页面元素有重复ID---注意
                ibpm.layerForm({
                    id: 'layer_' + dfop.id,
                    title: dfop.placeholder,
                    url: dfop.PageUrl,
                    width: dfop.width,
                    height: dfop.height,
                    maxmin: true,
                    callBack: function (id) {
                        return top[id].acceptClick($.BPM_GirdSelect.callback);
                    }
                });
            }
        },
        callback: function (item, id) {
            var $self = $('#' + id);
            var dfop = $self[0]._MGirdSelect.dfop;
            dfop._itemValue = dfop._itemValue || {};
            if (dfop._itemValue[dfop.value] != item[dfop.value]) {
                if (!!dfop.select) {
                    dfop.select(item);
                }
                $self.trigger('change');
            }

            if (!item) {
                item.text = dfop.placeholder;
            }
            else {
                $self.addClass('selected');
            }
            $self.find('span').text(item[dfop.text]);
            dfop._itemValue = item;
        }
    }

    $.fn.ibpm_GirdSelectRule = function (op) {
        var dfop = {
            placeholder: "请选择",
            icon: 'fa-plus',
            url: '',    //获取数据的地址
            PageUrl: '',
            Rule: true,
            selectWord: 'F_Name',
            headData: [{ label: "编号", name: "F_Code", width: 120, align: "left" },
                { label: "名称", name: "F_Name", width: 260, align: "left" },
                { label: "信息", name: "F_Nature", width: 160, align: "left" },
                { label: "备注", name: "F_Txt", width: 260, align: "left" }],
            value: 'F_No',
            text: 'F_Name',
            width: 1100,
            height: 700,
            select: false,     // 选择事件
            param: {},
            _loaded: false
        };

        $.extend(dfop, op || {});
        var $self = $(this);
        dfop.id = $self.attr('id');
        if (!dfop.id) {
            return false;
        }
        if (!!$self[0]._MGirdSelect) {
            return $self;
        }
        $self[0]._MGirdSelect = { dfop: dfop };
        $.BPM_GirdSelect.init($self);

        return $self;
    }

    $.fn.ibpm_GirdSelect = function (op) {
        var dfop = {
            placeholder: "请选择",
            icon: 'fa-plus',
            url: '',    //获取数据的地址
            PageUrl: '',    //选择交互的地址
            Rule: false,
            selectWord: '',
            headData: [],
            value: '',
            text: '',
            width: 800,
            height: 600,
            select: false,     // 选择事件
            param: {},
            _loaded: false
        };

        $.extend(dfop, op || {});
        var $self = $(this);
        dfop.id = $self.attr('id');
        if (!dfop.id) {
            return false;
        }
        if (!!$self[0]._MGirdSelect) {
            return $self;
        }
        $self[0]._MGirdSelect = { dfop: dfop };
        $.BPM_GirdSelect.init($self);

        return $self;
    }
    $.fn.ibpm_GirdSelectGet = function () {
        var $this = $(this);
        var dfop = $this[0]._MGirdSelect.dfop;
        dfop._itemValue = dfop._itemValue || {};
        var res = dfop._itemValue[dfop.value] || "";
        if (res == "")
            res = dfop._itemValue.value || "";

        return res;
    }
    $.fn.ibpm_GirdSelectSet = function (value) {
        var $this = $(this);
        var dfop = $this[0]._MGirdSelect.dfop;
        function set(_value) {
            if (_value == '') {
                dfop._itemValue = { value: "", text: dfop.placeholder };
                $this.removeClass('selected');
                $this.find('span').text(dfop._itemValue.text);
                if (!!dfop.select) {
                    dfop.select(dfop._itemValue);
                }
            }
            else {
                if (dfop._loaded) {
                    $.each(dfop._data, function (id, item) {
                        if (item[dfop.value] == _value) {
                            if (!!dfop.select) {
                                dfop.select(item);
                            }
                            $this.trigger('change');
                            $this.addClass('selected');
                            $this.find('span').text(item[dfop.text]);
                            dfop._itemValue = item;
                            return false;
                        }
                    });
                }
                else {
                    setTimeout(function () {
                        set(_value);
                    }, 200);
                }
            }
        }
        function getset(_value) {
            ibpm.httpAsync('POST', top.$.rootUrl + '/Home/GirdSelectData?id=' + _value, dfop.param, function (json) {
                //alert(JSON.stringify(json));
                $this.trigger('change');
                $this.addClass('selected');
                $this.find('span').text(json.text);
                dfop._itemValue = { value: json.value, text: json.text };
                return false;
            });
        }
        if (dfop.Rule) {
            getset(value);
        }
        else {
            set(value);
        }

    }
})(window.jQuery, top.ibpm);